<template>
  <div>
    <wxc-popup
      :height="getScreenHeight()-400"
      pos="bottom"
      ref="wxcpopup"
      :show="showMask"
      popup-color="transparent"
      @wxcPopupOverlayClicked="showMask=false"
    >
      <div class="shopAlert">
        <div class="alertTop">
          <image class="goodsImg" @click="showImg=true" :src="option_thumb" resize="cover" />
          <div class="goodsTit">
            <text class="goodsName">{{goodsName}}</text>
            <div class="rowPrice">
              <text class="symbol">AU$</text>
              <text class="price">{{option_price}}</text>
            </div>
            <!-- <text class="intro">{{goodInfo.title}}</text> -->
            <text class="intro" v-if="option_stock<1">Out of Stock</text>
            <text class="intro" v-if="option_stock<11&&option_stock>=1">Only Few in Stock</text>
            <text class="intro" v-if="option_stock>10">In Stock</text>
            <text class="intro" v-if="option_title">Selected: {{option_title}}</text>

            <text class="iconfont alertClose" @click="hidePop">&#xe608;</text>
          </div>
        </div>
        <scroller class="alertScroll">
          <div class="styleBox" v-for="(item,index) in shopSpec" :key="index">
            <text class="styleTitle">{{item.title}}</text>
            <div class="itemBox">
              <text
                class="styleItem"
                v-for="(itemSpe,indexs) in item.content"
                :key="indexs"
                :class="[itemSpe.id==selectSpec[index]?'styleActive':'']"
                @click="selectSp(index,itemSpe.id)"
              >{{itemSpe.name}}</text>
            </div>
          </div>
          <div class="price-number">
            <text class="num-title">Quantity</text>
            <div class="gnumber">
              <div class="numaction" @click="sub()">
                <image class="addImg" src="root:img/iconMius.png" />
              </div>
              <div class="numinputbox">
                <!-- <input class="numinput" type="number" v-model="number" /> -->
                <text class="numinput">{{number}}</text>
              </div>
              <div class="numaction" @click="add()">
                <image class="addImg" src="root:img/iconPlus.png" />
              </div>
            </div>
          </div>
        </scroller>
        <text class="sureBtn" @click="addToCart" v-if="goods_type=='1'">Add to cart</text>
        <text class="sureBtn" @click="toPay" v-if="goods_type=='2'">To Pay</text>
      </div>
    </wxc-popup>
    <div class="imgPre" v-if="showImg" @click="e=>{e.stopPropagation();showImg=false}">
      <image class="previewImg" :src="option_thumb" resize="contain" />
    </div>
  </div>
</template>

<script>
import { WxcPopup } from "weex-ui";

const AddCart = new BroadcastChannel("AddCart");
import {
  get_goods_info,
  get_goods_spec,
  get_goods_option,
  add_order_info,
  get_address_list,
  get_orderConfirm
} from "../mixin/ajax.js";
import asCore from "../mixin/core";

export default {
  props: {
    goods_type: {
      default: "1"
    },
    addressId: {
      default: ""
    },
    goodInfo: {
      default: {}
    },
    shopSpec: {
      default: []
    },
    option: {
      default: []
    },
    noOption: {
      default: false
    }
  },
  components: { WxcPopup },
  data() {
    return {
      userId: "",
      goodsName: "",
      showMask: false,
      showImg: false,
      selectSpec: [],
      number: 1,
      option_id: "",
      option_title: "",
      option_price: "",
      option_stock: 0,
      option_thumb: ""
    };
  },
  methods: {
    add() {
      if (this.number < this.option_stock) {
        this.number++;
      }
    },
    sub() {
      if (this.number > 1) {
        this.number--;
      }
    },
    selectSp(index, value) {
      if (!this.selectSpec[index] || value !== this.selectSpec[index]) {
        this.selectSpec.splice(index, 1, value);
      } else {
        this.selectSpec.splice(index, 1, "");
      }

      this.getOPtion();
    },
    getOPtion() {
      get_goods_option(
        {
          goods_id: this.goods_id,
          content: this.selectSpec.join(",")
        },
        (res, flag) => {
          if (flag) {
            if (res.code == 200) {
              if (res.data) {
                this.option_id = res.data.id;
                this.option_title = res.data.title;
                this.option_price = res.data.price;
                this.option_thumb = res.data.thumb;
                this.option_stock = res.data.stock;
              } else {
                this.option_id = "";
                this.option_title = "";
                this.option_price = this.goodInfo.price;
                this.option_thumb = this.goodInfo.thumb;
                this.option_stock = this.goodInfo.stock;
              }
            }
          }
        }
      );
    },

    addToCart() {
      if (!this.userId) {
        this.push("root:app/login/login.js");
        return false;
      }
      if (!this.noOption && !this.option_id) {
        this.toast("Please select a size");
        return false;
      }
      add_order_info(
        {
          users_id: this.userId,
          goods_id: this.goods_id,
          option_id: this.option_id,
          address_id: this.addressId,
          number: this.number
        },
        (res, flag) => {
          if (flag) {
            if (res.code == 200) {
              this.hidePop();
              AddCart.postMessage("update");
              this.toast(res.message);
            } else {
              this.alert(res.message);
            }
          }
        }
      );

      // this.toast("加入购物车");
    },
    toPay() {
      if (!this.userId) {
        this.push("root:app/login/login.js");
        return false;
      }
      if (!this.noOption && !this.option_id) {
        this.toast("Please select a size");
        return false;
      }
      let data = [
        {
          option_id: this.option_id,
          number: this.number,
          postage: this.goodInfo.postage,
          goodsId: this.goods_id,
          thumb: this.option_thumb,
          goodsName: this.goodsName,
          price: this.option_price,
          title: this.option_title,
          total_price: this.goodInfo.total_price,
          goods_type: this.goodInfo.goods_type
        }
      ];
      let postdate = {
        users_id: this.userId,
        goods_id: this.goods_id,
        number: this.number,
        option_id: this.option_id
      };
      get_orderConfirm(postdate, (res, flag) => {
        if (flag) {
          if (res.code == 200) {
            this.push("root:app/order/confirm.js", {
              data: JSON.stringify(data)
            });
          } else {
            this.alert(res.message);
          }
        }
      });
    },
    hidePop() {
      this.$refs.wxcpopup.hide();
    },
    initPage(id, goodsName) {
      this.goods_id = id;
      this.goodsName = goodsName;
      this.showMask = true;
      this.option_price = this.goodInfo.price;
      this.option_stock = this.goodInfo.stock;
      this.option_thumb = this.goodInfo.thumb;
      asCore.getBsessionid(userId => {
        if (userId) {
          this.userId = userId;
         
        }
      });
    }
  }
};
</script>

<style>
.shopAlert {
  flex: 1;
  background-color: #ffffff;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
}

.alertTop {
  flex-direction: row;
  flex-wrap: nowrap;
  padding-top: 32px;
  padding-left: 32px;
  padding-right: 32px;
  padding-bottom: 10px;
}

.goodsImg {
  width: 208px;
  height: 208px;
  background-color: #f2f2f2;
  border-radius: 8px;
  margin-right: 50px;
}

.goodsTit {
  flex: 1;
  height: 228px;
  position: relative;
}
.goodsName {
  font-size: 28px;
  padding-right: 30px;
}

.rowPrice {
  flex-direction: row;
  align-items: center;
  margin-top: 15px;
}

.symbol {
  font-size: 22px;
  color: #ec414d;
  margin-right: 5px;
}

.price {
  color: #ec414d;
  font-size: 36px;
}

.intro {
  lines: 4;
  width: 426px;
  margin-top: 14px;
  color: #666666;
  font-size: 26px;
  line-height: 40px;
  text-overflow: ellipsis;
}

.iconfont {
  font-family: iconfont;
}

.alertClose {
  width: 44px;
  height: 44px;
  text-align: center;
  line-height: 44px;
  position: absolute;
  right: 0;
  font-size: 30px;
  color: #666666;
}

.styleItem {
  border-width: 2px;
  border-style: solid;
  border-color: #e6eaed;
  background-color: #e6eaed;
  color: #333333;
  border-radius: 5px;
  height: 56px;
  line-height: 56px;
  padding: 0 30px;
  margin-right: 24px;
  font-size: 30px;
}

.alertScroll {
  flex: 1;
}

.styleBox {
  align-items: flex-start;
  padding: 20px 32px;

  border-bottom-width: 1px;
  border-bottom-style: solid;
  border-bottom-color: #e8e8e8;
}

.styleTitle {
  color: #1e1e1e;
  font-size: 30px;
}

.itemBox {
  flex-direction: row;
  margin-top: 16px;
}

.price-number {
  padding: 10px 32px;
  height: 100px;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  border-bottom-width: 1px;
  border-bottom-style: solid;
  border-bottom-color: #e8e8e8;
  margin-bottom: 30px;
}

.num-title {
  font-size: 30px;
  color: #1e1e1e;
}

.gnumber {
  flex-direction: row;
  align-items: center;
  height: 62px;
}

.styleActive {
  color: #30b158;
  border-color: #30b158;
  background-color: #ffffff;
}

.numaction {
  width: 60px;
  height: 60px;
  background-color: #f2f2f2;
  justify-content: center;
  align-items: center;
}
.addImg {
  width: 32px;
  height: 32px;
}

.numIcon {
  color: #1e1e1e;
  font-weight: 600;
}

.numinput {
  width: 120px;
  text-align: center;
  font-size: 44px;
  height: 62px;
  line-height: 62px;
  color: #ec414d;
}

.numinputbox {
  width: 120px;
  background-color: #fdfeff;
}

.sureBtn {
  width: 686px;
  height: 80px;
  margin-left: 32px;
  line-height: 80px;
  border-radius: 10px;
  margin-bottom: 14px;
  background-color: #303030;
  color: #fff;
  text-align: center;
  font-size: 30px;
}
.sureBtn:active {
  background-color: #000;
  color: #fff;
}
.imgPre {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.8);
}
.previewImg {
  width: 750px;
  height: 1200px;
}
</style>